package Homework;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;



public class CopyFolder {

	public static void main(String[] args) throws IOException{
		String src = "d://jixun";
		String desc = "copyfolder";
		copyFolder(src,desc);
	}

	private static void copyFolder(String src, String desc) throws IOException {
		File source = new File(src);
		File target = new File(desc);
		
		if(!target.exists()){
			target.mkdirs();
		}
		
		File[] listFiles = source.listFiles();
		for(File currentFile : listFiles){
			if(currentFile.isFile()){				
				copy(currentFile,target);
			}else{
				String childFolderPath = desc+"//"+currentFile.getName();
				copyFolder(currentFile.getAbsolutePath(), childFolderPath);
			}
		}		
		
	}

	private static void copy(File currentFile, File target) throws IOException {
		String childFilePath = target.getAbsolutePath()+"//"+currentFile.getName(); 
		FileInputStream input = new FileInputStream(currentFile);
		FileOutputStream output = new FileOutputStream(childFilePath);
		
		byte[] b = new byte[1024];
		int len = 0;
		while((len=input.read(b))!=-1){
			output.write(b,0,len);
			output.flush();
		}
		input.close();
		output.close();
	}

}
